package org.pointex.service.impl;

import java.io.Serializable;
import java.util.Date;

import org.pointex.common.service.impl.BaseServiceImpl;
import org.pointex.common.util.constant.Constant;
import org.pointex.dao.impl.CreditsRecordDaoImpl;
import org.pointex.dao.impl.UsersDaoImpl;
import org.pointex.domain.CreditsRecord;
import org.pointex.domain.Users;
import org.pointex.service.MaxNoService;
import org.pointex.service.UsersService;
import org.pointex.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * @ClassName: ConvertRuleDetailServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午4:02:01
 *
 */
@Service("UsersService")
public class UsersServiceImpl extends BaseServiceImpl<Users, Serializable>
		implements UsersService {

	@Autowired
	private UsersDaoImpl dao;

	@Autowired
	private CreditsRecordDaoImpl creditsRecordDaoImpl;

	@Autowired
	private MaxNoService maxNoService;

	/**
	 * param use 用户 param credits 更新积分 param status 1=添加积分 2=消费积分 param actionid
	 * 积分操作行为编号 param Content 积分记录内容
	 */
	@Override
	public boolean updateCredits(Users users, int credits, int status,
			String actionid, String Content) {
		boolean result = false;
		try {
			// 添加积分(更新用户积分)
			if (status == 1) {
				users.setCredits(users.getCredits() + credits);
			} else if (status == 2) {// 消费积分
				if (users.getCredits() >= credits) {
					users.setCredits(users.getCredits() - credits);
				}
			}
			int updateresult = dao.updateByPrimaryKeySelective(users);// 更新用户积分

			// 写入每次规则添加积分的记录
			CreditsRecord creditsRecord = new CreditsRecord();
			creditsRecord.setCreditsRecordId(maxNoService
					.updateGetNextNo(Constant.MAXNO_CTR_NO));// 自动生成积分记录编号
			creditsRecord.setCreditsBehaviorId(actionid);
			creditsRecord.setUserId(users.getUserId());
			if (status == 1)
				creditsRecord.setCreditsState("添加积分");
			else if (status == 2)
				creditsRecord.setCreditsState("消费积分");
			creditsRecord.setCreditsContent("");
			creditsRecord.setCreditsValue(credits);
			creditsRecord.setCreateDt(DateUtil
					.formatDate_YYYYMMDDHHMMSS(new Date()));
			creditsRecord.setUpdateDt(DateUtil
					.formatDate_YYYYMMDDHHMMSS(new Date()));
			creditsRecord.setCreateOper(users.getUserId());
			creditsRecord.setUpdateOper(users.getUserId());
			creditsRecord.setCreditsContent(Content);
			creditsRecord.setStatus(new Byte((byte) 1));
			creditsRecord.setDelFlag(new Byte((byte) 0));
			int insertresult = creditsRecordDaoImpl.insert(creditsRecord);

			if (updateresult + insertresult >= 2)
				result = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

}
